<?php
header("Content-type: text/html; charset=utf-8");
//加载Library
date_default_timezone_set('Asia/Chongqing');
define('ROOT_PATH', dirname(__FILE__));
$env = getenv('RUNTIME_ENVIROMENT') ? getenv('RUNTIME_ENVIROMENT') : 'local';
define("RUNTIME_ENVIROMENT", $env);
require_once ROOT_PATH . '/../../library/bootstrap.php';


//读取数据
$params = $_REQUEST;
if(empty($params)){
//
    $return = [
        'code' => '4001',
        'message' => '接受数据为空',
    ];
    Func_Log::write('request', 'error', 'params =' . json_encode($params) . "return  = " . json_encode($return));
    echo json_encode($return);
    exit;

}
$line = empty($params['line']) ? $params['line'] : 1;
//校验数据完整性
if(!Lib_Notice::checkData($params,array('callPhone'))){
    $return = [
        'code' => '4002',
        'message' => '缺少必要参数',
    ];
    Func_Log::write('request', 'error', 'params =' . json_encode($params) . "return  = " . json_encode($return));
    echo json_encode($return);
    exit;
}
//通过手机号查询案件
if(empty($params['idCard'])){

    if($params['callPhone'] == 18611748482 || $params['callPhone'] == 17310698030){
        $return = [
            'code'=>'200',
            'message'=>'有负责调解员，指定坐席',
            'exten' => '8000',
        ];
        Lib_Redis_String::setex($params['callPhone'],3600,$params['idCard']);
        Func_Log::write('request', 'success','params ='.json_encode($params) ."return  = ".json_encode($return));
        echo json_encode($return);
        exit;
    }
    $caseData = Module_Mediate_CaseLitigant::getInstance()->getAllBySearch(['caseId'],['where'=>["telephone={$params['callPhone']}"]]);
    if(empty($caseData)){
        $return = [
            'code' => '3001',
            'message' => '来电号码对应案件的调解员信息',
        ];
        Func_Log::write('request', 'error', 'params =' . json_encode($params) . "return  = " . json_encode($return));
        echo json_encode($return);
        exit;
    }
    $case_ids = implode(',',array_unique(array_column($caseData,'caseId')));
    $case = Module_Mediate_Case::getInstance()->getAllBySearch(['mediatorId'],['where'=>["id in ($case_ids)",'mediatorId>0'],'order'=>'id DESC']);
    if(empty($case)){
        $return = [
            'code' => '3001',
            'message' => '来电号码对应案件的调解员信息',
        ];
        Func_Log::write('request', 'error', 'params =' . json_encode($params) . "return  = " . json_encode($return));
        echo json_encode($return);
        exit;
    }
    $admin_id = $case[0]['mediatorId'];
}else{
    if(in_array($params['idCard'],['12345678'])){
        $return = [
            'code'=>'200',
            'message'=>'有负责调解员，指定坐席',
            'exten' => '8000',
        ];
        Lib_Redis_String::setex($params['callPhone'],3600,$params['idCard']);
        Func_Log::write('request', 'success','params ='.json_encode($params) ."return  = ".json_encode($return));
        echo json_encode($return);
        exit;
    }
    if(strlen($params['idCard']) == 17){
        $params['idCard'] = $params['idCard']."X";
    }
    $caseData = Module_Mediate_CaseLitigant::getInstance()->getAllBySearch(['caseId'],['where'=>["cardId='{$params['idCard']}'"]]);
    if(empty($caseData)){
        $return = [
            'code' => '3000',
            'message' => '身份证号码未查询到案件请重新输入',
        ];
        Func_Log::write('request', 'error', 'params =' . json_encode($params) . "return  = " . json_encode($return));
        echo json_encode($return);
        exit;
    }
    $case_ids = implode(',',array_unique(array_column($caseData,'caseId')));
    $case = Module_Mediate_Case::getInstance()->getAllBySearch(['mediatorId'],['where'=>["id in ($case_ids)",'mediatorId>0'],'order'=>'id DESC']);
    if(empty($case)){
        $return = [
            'code' => '3000',
            'message' => '身份证号码未查询到案件请重新输入',
        ];
        Func_Log::write('request', 'error', 'params =' . json_encode($params) . "return  = " . json_encode($return));
        echo json_encode($return);
        exit;
    }
    Lib_Redis_String::setex($params['callPhone'],3600,$params['idCard']);
    $admin_id = $case[0]['mediatorId'];
}
if($admin_id){
    $admin = Module_User_Administrator::getInstance()->getItemById($admin_id);
    if(!empty($admin) && $admin['status'] == 1 && (!empty($admin['workerId']) || !empty($admin['workerId2']) )){
        $return = [
            'code'=>'200',
            'message'=>'有负责调解员，指定坐席',
            'exten' => $line == 1 ? $admin['workerId'] : $admin['workerId2'],

        ];
        Func_Log::write('request', 'success','params ='.json_encode($params) ."return  = ".json_encode($return));
        echo json_encode($return);
        exit;
    }else{
        $return = [
            'code'=>'200',
            'message'=>'有负责调解员，指定坐席',
            'exten' => '8000',

        ];
        Func_Log::write('request', 'error admin error'.$admin_id,'params ='.json_encode($params) ."return  = ".json_encode($return));
        echo json_encode($return);
        exit;
    }
}else{
    $return = [
        'code'=>'200',
        'message'=>'有负责调解员，指定坐席',
        'exten' => '8000',

    ];
    Func_Log::write('request', 'error admin error'.$admin_id,'params ='.json_encode($params) ."return  = ".json_encode($return));
    echo json_encode($return);
    exit;
}
